<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of meals
 *
 * @author cs321ga3a
 */
class meals extends CI_Controller{
    
    function getMeals() {

        $url = kitchen_base_url()."todaysMeals";
        $args = array(
                'user_id' => $this->session->userdata('user_id')
                );
                
		$result = $this->curl->simple_post($url, $args);
		
		$xml = new SimpleXMLElement($result);
    
        $meals = $xml->meal;
        $data = array(      
            'title' => 'Meals',
            'payload' => $meals,
            'view' => 'view_tonight',
			'footer_menu' => true,
			'username'=> $this->session->userdata('username')
        );
        
        
        $this->load->view('view_template', $data);   
        
    }
    
    function buttonPressed() {
        $meal_id = $this->input->post('meal_id');
        $type = $this->input->post('type');
        
        $return_val = "";
        
        if ($type=='order'){
        	$comment = $this->input->post("comment");
			if ($comment == null){
				$comment = "";
			}
        }
       
	   if($type=='order') {
            $url = kitchen_base_url().'order';
            $args = array(
                'uid' => $this->session->userdata('user_id'),
                'meal_id' => $meal_id,
                'comment' => $comment);
            $result = $this->curl->simple_post($url, $args);
			
			if($result=="good"){
				echo "good";
				return;
			}
			else if ($result=="There was an error server side."){
				echo "fail";
				return;
			}
			else if($result=="Past the cut-off time for meal ordering."){
				echo "time";
				return;
			}
			else {
				$data = new SimpleXMLElement($result);
				$return_val = "Allergies Found:";
				foreach($data->allergy as $x) {
					$return_val .= "\n".$x->name;
				}
				$return_val .= "\n";
			}
		}
        else if ($type=='vote') {
            $url = kitchen_base_url().'vote';
            $args = array(
                'uid' => $this->session->userdata('user_id'),
                'meal_id' => $meal_id);
            $result = $this->curl->simple_post($url, $args);
						
			if($result=="good"){
				echo "good";
				return;
			}
			else if ($result=="There was an error server side."){
				echo "fail";
				return;
			}
			else if($result == "Past the cut-off time for meal voting."){
				echo "time";
				return;
			}
			else {
				$data = new SimpleXMLElement($result);
				$return_val = "Allergies Found:";
				foreach($data->allergy as $x) {
					$return_val .= "\n".$x->name;
				}
				$return_val .= "\n";
			}
		}
		echo $return_val;
    }

    function forceChoice() {
        $meal_id = $this->input->post('meal_id');
        $type = $this->input->post('type');
        
       	if ($type=='order'){
        	$comment = $this->input->post("comment");
        }
        else{
        	$comment = "";
        }
        
        $return_val = "";
       
	   //Choose URL dependant on vote or order
	   if($type=='order') 
            $url = kitchen_base_url().'overrideOrder';
		else if($type=='vote') 
			$url = kitchen_base_url().'overrideVote';

		$args = array(
			'uid' => $this->session->userdata('user_id'),
			'meal_id' => $meal_id,
			'comment' => $comment);
		$result = $this->curl->simple_post($url, $args);
		if($result=="good"){
			$return_val = "good";
		}
		else{
			$return_val = "fail";
		}
		echo $return_val;
	}
    
    function index() {
        $this->getMeals();
    }
    
}

?>
